﻿using Coolzon.UI.Core.Runtime.Script;
using Coolzon.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using RT = Coolzon.Metadata.Runtime;

namespace Coolzon.UI.Core.Parts.Widgets
{
    /// <summary>
    /// 模板容器
    /// </summary>
    public class TemplateHolder : UserControl
    {
        private RT.Template _templateMetadata;
        /// <summary>
        /// 容器显示模板实例的条件（javascript表达式)
        /// </summary>
        private string _condition;

        protected Widget _templatePart;
        /// <summary>
        /// 所属的组件
        /// </summary>
        protected Widget _ownerWidget;
        protected DataResult _dataResult;
        protected DataSeries _dataSeries;
        protected DataItem _dataItem;

        private Grid _layout;

        public TemplateHolder()
        {
            _layout = new Grid();
            this.Content = _layout;
        }

        internal void Init(Widget ownerWidget,
            DataResult dataResult,
            DataSeries dataSeries,
            DataItem dataItem)
        {
            this._ownerWidget = ownerWidget;
            this._dataResult = dataResult;
            this._dataSeries = dataSeries;
            this._dataItem = dataItem;
        }

        public void ValidateCondition()
        {
            if (this._dataItem != null && string.IsNullOrEmpty(this._dataItem.Value))
            {
                this.Visibility = System.Windows.Visibility.Collapsed;
                return;
            }

            // 检查组件是否可以显示
            ScriptEngine scriptEngine = this._ownerWidget.PartContext.ScriptEngine;
            try
            {
                if (scriptEngine.RunDataExpressionToBoolean(this._condition,
                    _dataSeries,
                    _dataItem) == true)
                {
                    this.Visibility = System.Windows.Visibility.Visible;
                    OnDisplay();
                }
                else
                {
                    this.Visibility = System.Windows.Visibility.Collapsed;
                }
            }
            catch
            {
                this.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public virtual void SetProperty(string propertyName, object value)
        {
            if (propertyName == "Condition")
            {
                _condition = value.ToString();
            }
            else if (propertyName == "Template")
            {
                _templateMetadata = ((RT.Template)value);
            }
        }

        protected void OnDisplay()
        {
            if (_templateMetadata != null)
            {
                TemplateInstance templateInstance = TemplateInstanceCache.Reference(this._ownerWidget, _templateMetadata.Content);
                if (templateInstance != null)
                {
                    _templatePart = templateInstance.Instance;
                    //if (_templatePart is DataItemPart)
                    //{
                    //    (_templatePart as DataItemPart).DataResult = _dataResult;
                    //    (_templatePart as DataItemPart).XDataSeriesId = _dataSeries.Id;
                    //    (_templatePart as DataItemPart).XDataItemId = _dataItem.Id;
                    //    (_templatePart as DataItemPart).Active(this._part.PartContext);
                    //    _ctlLayout.Children.Add(_templatePart);
                    //}
                    //else if (_templatePart is DataPart)
                    //{
                    //    (_templatePart as DataPart).DataResult = _dataResult;
                    //    (_templatePart as DataPart).Active(this._part.PartContext);
                    //    _ctlLayout.Children.Add(_templatePart);
                    //}
                    _layout.Children.Add(_templatePart);
                }
            }
        }

        public void Deactive()
        {
            if (_templatePart != null)
            {
                _templatePart.Deactive();
                _layout.Children.Remove(_templatePart);
            }
        }
    }
}
